home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 4 / Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso / Development / Source / Macintosh Tracker Source / Tracker Client Folder / CMyApplication.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-08  |  8.4 KB  |  344 lines  |  [TEXT/KAHL]

  1. /* CMyApplication.c */
  2.  
  3. #include "CMyApplication.h"
  4. #include "MenuController.h"
  5. #include "CMyDocument.h"
  6. #include "CSack.h"
  7. #include "Memory.h"
  8. #include "Alert.h"
  9. #include "StandardFile.h"
  10. #include "File.h"
  11.  
  12.  
  13. #define AppleMenuID (256)
  14. #define FileMenuID (257)
  15. #define EditMenuID (258)
  16. #define StuffMenuID (259)
  17. #define TrackerMenuID (260)
  18.  
  19. #define NotEnoughMemoryID (130L*65536L + 2)
  20.  
  21. #define CantOpenMultipleSongListsID (5120L*65536L + 3)
  22.  
  23. #define CantFindServer (4096L*65536L + 1)
  24. #define CantSearchForServer (4096L*65536L + 2)
  25. #define CantLaunchServer (4096L*65536L + 4)
  26.  
  27. #define RemoteFatalErrorTemplate (4097L*65536L)
  28.  
  29.  
  30. /* */            CMyApplication::CMyApplication()
  31.     {
  32.         FSSpec            PrefsFile;
  33.         short                VRefNum;
  34.         long                DirID;
  35.         short                Error;
  36.         short                LocalRefNum;
  37.         long                SizeOfAlias;
  38.         Handle            Temp;
  39.  
  40.         Error = AEInstallEventHandler(kCoreEventClass,
  41.             kAEApplicationDied,&MyHandleOBIT,0,False);
  42.  
  43.         Error = AEInstallEventHandler(ControlEventClass,
  44.             ErrorEvent,&MyHandleERROR,0,False);
  45.  
  46.     }
  47.  
  48.  
  49. void            CMyApplication::InitMenuBar(void)
  50.     {
  51.         APRINT(("+CMyApplication::InitMenuBar"));
  52.         PostMenuToBar(AppleMenuID);
  53.         PostMenuToBar(FileMenuID);
  54.         PostMenuToBar(EditMenuID);
  55.         PostMenuToBar(StuffMenuID);
  56.         PostMenuToBar(TrackerMenuID);
  57.         APRINT(("-CMyApplication::InitMenuBar"));
  58.     }
  59.  
  60.  
  61. void            CMyApplication::DispatchOpenDocument(FSSpec* TheFSSpec)
  62.     {
  63.         CMyDocument*    ADoc;
  64.         FInfo                    FileInfo;
  65.  
  66.         /* identifying document */
  67.         FSpGetFInfo(TheFSSpec,&FileInfo);
  68.         if (FileInfo.fdType == FILETYPE1)
  69.             {
  70.                 /* it's one of our preferences files */
  71.                 if (ListOfDocuments->NumElements() == 0)
  72.                     {
  73.                         /* make a new one */
  74.                         if (!FirstMemCacheValid())
  75.                             {
  76.                                 AlertError(NotEnoughMemoryID,NIL);
  77.                                 return;
  78.                             }
  79.                         ADoc = new CMyDocument;  /* create the document */
  80.                         ADoc->DoOpenFile(TheFSSpec);  /* make it link to a file */
  81.                     }
  82.                  else
  83.                     {
  84.                         AlertError(CantOpenMultipleSongListsID,NIL);
  85.                     }
  86.             }
  87.         else
  88.             {
  89.                 /* assume it is a tracker file */
  90.                 if ((FileInfo.fdType == '\?\?\?\?') || (FileInfo.fdType == 'TEXT')
  91.                     || (FileInfo.fdType == 'BINA') || (FileInfo.fdCreator == '\?\?\?\?'))
  92.                     {
  93.                         /* change file type */
  94.                         FileInfo.fdType = 'STrk';
  95.                         FileInfo.fdCreator = CREATORCODE;
  96.                         FSpSetFInfo(TheFSSpec,&FileInfo); /* no big loss if it fails... */
  97.                     }
  98.                 if (ListOfDocuments->NumElements() == 0)
  99.                     {
  100.                         /* no preference file, create a new one */
  101.                         DoMenuNew();
  102.                     }
  103.                 /* dispatch it to the document */
  104.                 ListOfDocuments->ResetScan();
  105.                 ListOfDocuments->GetNext(&ADoc);
  106.                 ADoc->AddSong(TheFSSpec);
  107.             }
  108.     }
  109.  
  110.  
  111. /* open a file using the standard file things */
  112. void                CMyApplication::DoMenuOpen(void)
  113.     {
  114.         FSSpec            FileInfo;
  115.  
  116.         if (ListOfDocuments->NumElements() == 0)
  117.             {
  118.                 inherited::DoMenuOpen();
  119.             }
  120.          else
  121.             {
  122.                 if (!FirstMemCacheValid())
  123.                     {
  124.                         AlertError(NotEnoughMemoryID,NIL);
  125.                         return;
  126.                     }
  127.                 if (FGetFile(&FileInfo,NIL,NIL,-1))
  128.                     {
  129.                         DispatchOpenDocument(&FileInfo);
  130.                     }
  131.             }
  132.     }
  133.  
  134.  
  135. void                CMyApplication::DoMenuNew(void)
  136.     {
  137.         if (ListOfDocuments->NumElements() == 0)
  138.             {
  139.                 inherited::DoMenuNew();
  140.             }
  141.     }
  142.  
  143.  
  144. void                CMyApplication::EnableMenuItems(void)
  145.     {
  146.         if (ListOfDocuments->NumElements() == 0)
  147.             {
  148.                 MyEnableItem(mFileNew);
  149.             }
  150.         MyEnableItem(mFileOpen);
  151.         MyEnableItem(mAppleAbout);
  152.         MyEnableItem(mFileQuit);
  153.     }
  154.  
  155.  
  156. void                CMyApplication::LaunchTracker(OSType TrackerCreator)
  157.     {
  158.         FSSpec                                Tracker;
  159.         LaunchParamBlockRec        Launcher;
  160.         DTPBRec                                DesktopParamBlock;
  161.         PString                                NameOut;
  162.         short                                    Error;
  163.         short                                    DesktopRefNum;
  164.         short                                    CurResFileValue;
  165.  
  166.         StackSizeTest();
  167.         /* opening database */
  168.         NameOut[0] = 0;
  169.         DesktopParamBlock.ioNamePtr = NameOut;
  170.         CurResFileValue = CurResFile();
  171.         Error = GetVRefNum(CurResFileValue,&DesktopParamBlock.ioVRefNum);
  172.         PBDTGetPath(&DesktopParamBlock);
  173.         Error = DesktopParamBlock.ioResult;
  174.         if (Error != noErr)
  175.             {
  176.                 CMyDocument*        Document;
  177.  
  178.                 ListOfDocuments->ResetScan();
  179.                 ListOfDocuments->GetNext(&Document);
  180.                 /* must call stop first otherwise a nasty race condition develops */
  181.                 /* because the document would still get idle events & try to call */
  182.                 /* this procedure again */
  183.                 Document->DoStop();
  184.                 AlertError(CantSearchForServer,NIL);
  185.                 return;
  186.             }
  187.         DesktopRefNum = DesktopParamBlock.ioDTRefNum;
  188.  
  189.         /* finding tracker program */
  190.         DesktopParamBlock.ioCompletion = NIL;
  191.         DesktopParamBlock.ioNamePtr = NameOut;
  192.         DesktopParamBlock.ioDTRefNum = DesktopRefNum;
  193.         DesktopParamBlock.ioIndex = 0;
  194.         DesktopParamBlock.ioFileCreator = TrackerCreator;
  195.         PBDTGetAPPLSync(&DesktopParamBlock);
  196.         Error = DesktopParamBlock.ioResult;
  197.         if (Error != noErr)
  198.             {
  199.                 CMyDocument*        Document;
  200.  
  201.                 ListOfDocuments->ResetScan();
  202.                 ListOfDocuments->GetNext(&Document);
  203.                 Document->DoStop();
  204.                 AlertError(CantFindServer,NIL);
  205.                 return;
  206.             }
  207.         FMakeFSSpec(0,DesktopParamBlock.ioAPPLParID,NameOut,&Tracker);
  208.  
  209.         Launcher.launchBlockID = extendedBlock;
  210.         Launcher.launchEPBLength = extendedBlockLen;
  211.         Launcher.launchFileFlags = 0;
  212.         Launcher.launchControlFlags = launchNoFileFlags | launchContinue
  213.             | launchUseMinimum | launchDontSwitch;
  214.         Launcher.launchAppSpec = &Tracker;
  215.         Launcher.launchAppParameters = NIL;
  216.         Error = LaunchApplication(&Launcher);
  217.         if (Error != noErr)
  218.             {
  219.                 CMyDocument*        Document;
  220.  
  221.                 ListOfDocuments->ResetScan();
  222.                 ListOfDocuments->GetNext(&Document);
  223.                 Document->DoStop();
  224.                 AlertError(CantLaunchServer,NIL);
  225.                 return;
  226.             }
  227.          else
  228.             {
  229.                 CMyDocument*        Document;
  230.  
  231.                 ListOfDocuments->ResetScan();
  232.                 ListOfDocuments->GetNext(&Document);
  233.                 Document->PlayerLaunchedNotification(Launcher.launchProcessSN);
  234.             }
  235.     }
  236.  
  237.  
  238. void            CMyApplication::SendMessage(ProcessSerialNumber PlayerSN, AppleEvent* Message)
  239.     {
  240.         AppleEvent            Reply;
  241.         OSErr                        Error;
  242.  
  243.         StackSizeTest();
  244.         Error = AESend(Message,&Reply,kAENoReply,kAENormalPriority,kNoTimeOut,NIL,NIL);
  245.     }
  246.  
  247.  
  248. void            CMyApplication::KillPlayer(ProcessSerialNumber PlayerSN)
  249.     {
  250.         AEAddressDesc            AddressDescriptor;
  251.         OSErr                            Error;
  252.         AppleEvent                Event;
  253.  
  254.         Error = AECreateDesc(typeProcessSerialNumber,(void*)&PlayerSN,
  255.             sizeof(ProcessSerialNumber),&AddressDescriptor);
  256.         Error = AECreateAppleEvent(kCoreEventClass,kAEQuitApplication,&AddressDescriptor,
  257.             kAutoGenerateReturnID,kAnyTransactionID,&Event);
  258.         SendMessage(PlayerSN,&Event);
  259.         Error = AEDisposeDesc(&AddressDescriptor);
  260.         Error = AEDisposeDesc(&Event);
  261.     }
  262.  
  263.  
  264. static    pascal    OSErr    CMyApplication::MyHandleOBIT(AppleEvent* theAppleEvent,
  265.                                                 AppleEvent* reply, long handlerRefcon)
  266.     {
  267.         long                                    RemoteError;
  268.         DescType                            Stupid;
  269.         OSErr                                    Error;
  270.         long                                    ActualStupidSize;
  271.         ProcessSerialNumber        PSN;
  272.  
  273.         Error = AEGetParamPtr(theAppleEvent,keyErrorNumber,typeLongInteger,
  274.             &Stupid,(void*)&RemoteError,sizeof(long),&ActualStupidSize);
  275.  
  276.         Error = AEGetParamPtr(theAppleEvent,keyProcessSerialNumber,typeProcessSerialNumber,
  277.             &Stupid,(void*)&PSN,sizeof(ProcessSerialNumber),&ActualStupidSize);
  278.  
  279.         Error = MyGotRequiredParams(theAppleEvent);
  280.  
  281.         if (Error != noErr)
  282.             {
  283.                 return Error;
  284.             }
  285.          else
  286.             {
  287.                 if (Application->ListOfDocuments->NumElements() != 0)
  288.                     {
  289.                         CMyDocument*            Document;
  290.  
  291.                         Application->ListOfDocuments->ResetScan();
  292.                         Application->ListOfDocuments->GetNext(&Document);
  293.                         Document->PlayerDiedNotification();
  294.                     }
  295.                 return noErr;
  296.             }
  297.     }
  298.  
  299.  
  300. static    pascal    OSErr    CMyApplication::MyHandleERROR(AppleEvent* theAppleEvent,
  301.                                                 AppleEvent* reply, long handlerRefcon)
  302.     {
  303.         short                                    RemoteError;
  304.         DescType                            Stupid;
  305.         OSErr                                    Error;
  306.         long                                    ActualStupidSize;
  307.  
  308.         Error = AEGetParamPtr(theAppleEvent,keyErrorIDNum,typeShortInteger,
  309.             &Stupid,(void*)&RemoteError,sizeof(short),&ActualStupidSize);
  310.  
  311.         Error = MyGotRequiredParams(theAppleEvent);
  312.  
  313.         if (Error != noErr)
  314.             {
  315.                 return Error;
  316.             }
  317.          else
  318.             {
  319.                 CMyDocument*        Document;
  320.  
  321.                 Application->ListOfDocuments->ResetScan();
  322.                 if (Application->ListOfDocuments->GetNext(&Document))
  323.                     {
  324.                         Document->DoStop();
  325.                     }
  326.                 switch (RemoteError)
  327.                     {
  328.                         case FatalErrorOutOfMemory:
  329.                         case FatalErrorInternalError:
  330.                         case FatalErrorCantOpenCompressedFiles:
  331.                         case FatalErrorCouldntOpenFile:
  332.                         case FatalErrorCouldntCloseFile:
  333.                         case FatalErrorNotASong:
  334.                         case FatalError68020NeededID:
  335.                             AlertError(RemoteFatalErrorTemplate + RemoteError,NIL);
  336.                             break;
  337.                         default:
  338.                             AlertError(RemoteFatalErrorTemplate + FatalErrorUnknown,NIL);
  339.                             break;
  340.                     }
  341.                 return noErr;
  342.             }
  343.     }
  344.